package info.ideatower.compass;

import info.ideatower.infra.support.web.query.spring.PageArgumentResolver;
import info.ideatower.infra.support.web.query.spring.QueryParameterArgumentResolver;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Repository;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * 配置项
 */
@Configuration
public class AppConfiguration extends WebMvcConfigurerAdapter {

    /**
     * 拦截器配置
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
//        registry.addInterceptor(new TokenInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/error").setViewName("error.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    /**
     * Controller 注入方法参数
     * @param argumentResolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        // 查询参数器
        argumentResolvers.add(new QueryParameterArgumentResolver());
        // 分页参数器
        argumentResolvers.add(new PageArgumentResolver());
        super.addArgumentResolvers(argumentResolvers);
    }


    /**
     * 全局异常处理类
     * @return
     */
    @Bean
    public GlobalHandlerExceptionResolver globalHandlerExceptionResolver() {
        return new GlobalHandlerExceptionResolver();
    }

    @Bean("mapperScannerConfigurer")
    public MapperScannerConfigurer getMapperScannerConfigurer() {

        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("info.ideatower.compass.dao");
		mapperScannerConfigurer.setAnnotationClass(Repository.class);
		return mapperScannerConfigurer;
    }


}
